import Kurento from "kurento-client";
import {lConfig} from "../config/config";

const { wsUri } = lConfig.kurentoServer;

export default (callback) => {
  let client = null;

  return {
    /**
     * 获取 Kurento Client
     *
     * @param callback
     */
    getClient(callback) {
      if (client !== null) {
        return callback(null, client);
      }

      return Kurento(wsUri, (error, kurentoClient) => {
        if (error) {
          console.log("Could not find media server at address " + wsUri);
          return callback("Could not find media server at address" + wsUri
            + ". Exiting with error " + error);
        }

        client = kurentoClient;
        callback(null, kurentoClient);

        return kurentoClient
      });
    },

    /**
     * 将 fromEndpoint 的流连接到 toEndpoint
     *
     * @param fromEndpoint
     * @param toEndpoint
     */
    connect(fromEndpoint, toEndpoint) {
      return fromEndpoint.connect(toEndpoint, (error) => {
        if (error) {
          return callback && callback(error);
        }
        return
      })
    },

    /**
     * 初始化一个端点
     *
     * @param client
     * @param cb
     */
    initEndpoint (client, cb) {
      this.createMediaPipeline(client, (pipeline) => {
        this.createEndpoint(pipeline, (endpoint) => {
          // 回调
          if (cb) cb(endpoint, pipeline)
        })
      });
    },

    initPlayerEndpoint (client, uri, cb) {
      return this.createMediaPipeline(client, (pipeline) => {
        return this.createPlayerEndpoint(pipeline, uri, (player) => {
          // 回调
          if (cb) cb(player, pipeline)
          return { player, pipeline }
        })
      });
    },

    onIceCandidate (webRtcEndpoint, cb) {
      // IceCandidateFound OnIceCandidate
      return webRtcEndpoint.on('IceCandidateFound', (event) => {
        var iceCandidate = Kurento.getComplexType('IceCandidate')(event.candidate);
        cb && cb(iceCandidate);
        return iceCandidate
      });
    },

    createMediaPipeline (client, cb) {
      return client.create('MediaPipeline', (error, pipeline) => {
        if (error) {
          return callback && callback(error);
        }
        cb && cb(pipeline)
        return pipeline
      })
    },

    createEndpoint (pipeline, cb?) {
      return pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
        if (error) {
          return callback && callback(error);
        }
        cb && cb(webRtcEndpoint)
        return webRtcEndpoint
      })
    },

    createPlayerEndpoint (pipeline, uri, cb?) {
      return pipeline.create('PlayerEndpoint', { uri }, (error, player) => {
        if (error) {
          return callback && callback(error);
        }
        cb && cb(player)
        return player
      })
    },

    createRecordEndpoint (pipeline, uri, cb?) {
      return pipeline.create('RecorderEndpoint', { uri }, (error, recorder) => {
        if (error) {
          return callback && callback(error);
        }
        cb && cb(recorder)
        return recorder
      })
    },

    processOffer (endpoint, sdpOffer, cb) {
      endpoint.processOffer(sdpOffer, (error, sdpAnswer) => {
        if (error) {
          return callback && callback(error);
        }
        cb && cb(sdpAnswer);
      });

      endpoint.gatherCandidates((error) => {
        if (error) {
          return callback && callback(error);
        }
      });
    },

    stop(members, socket, userId) {
      /*for (let __userId in members) {
        var member = members[__userId] as Member;

        if (members[__userId] === userId) {
          socket.to(member.socketId).emit('stopCommunication');
          if (member.pipeline) {
            member.pipeline.release();
          }

          this.removeMember(__userId);
          // this.viewers = {};
        }

        (member[__userId] as Member).endpoint.release();
        delete member[__userId];
      }*/

      /*if (this.getMembers().length < 1) {
        console.log('Closing kurento client');
        if (this.client) {
          this.client.close();
        }
        this.client = null;
      }*/
    }
  }
}

